import Vue from 'vue'
import App from './App'
import util from './common/util'
import uView from './uni_modules/uview-ui'
import {
	getAccToken,
	getRefToken,
	setAccToken,
	setRefToken,
} from 'userToken.js'
import wxApi from './utils/wxApi.js'


Vue.prototype.$util = util
Vue.prototype.$wxShare = wxApi.wexinShare
Vue.prototype.$wxHide = wxApi.wexinHide

Vue.use(uView);


Vue.config.productionTip = false
App.mpType = 'app'
Vue.prototype.serverUrl = "https://api.orderfood.yibaow.cn";
// Vue.prototype.serverUrl = "http://192.168.1.39"; 
// Vue.prototype.serverUrl = "http://ybtest.natapp1.cc";
Vue.prototype.totalUrl = "http://ffgh.ff.yibaow.cn";

//封装this.uni_request方法  
Vue.prototype.uni_request = function(params) {
	params.method = params.method == undefined ? "GET" : params.method;
	let oriHeader = {}
	if (params.header) {
		oriHeader = params.header
	}
	params.header = Object.assign({
		// 'id_type':'user',
		'Content-Type': 'application/json;charset=utf-8',
		'token': getAccToken(),
	}, oriHeader)
	//在请求头中携带用户token信息
	if (!getAccToken()) {

	} else {
		uni.request({
			url: this.serverUrl + params.url,
			data: params.data,
			header: params.header,
			method: params.method,
			success: (res) => {
				//code === 200
				if (res.data.code == 200 || res.data.code == 0) {
					params.success(res)
				} else if (res.data.code == 403) { //刷新token
					uni.request({
						url: '/user/userLogin/refreshToken',
						method: 'POST',
						header: {
							'Content-Type': 'application/json;charset=utf-8',
							'refreshToken': getRefToken()
						},
						success(res) {
							if (res.data.code == 200 || res.data.code == 0) {
								setAccToken(res.data.data.token)
								setRefToken(res.data.data.refreshToken)
								//接口重调用
								uni.request({
									url: this.serverUrl + params.url,
									data: params.data,
									header: params.header,
									method: params.method,
									success: (res) => {
										if (res.data.code == 200 || res.data.code ==0) {
											params.success(res)
										}
									}
								})
							}
						}
					})
				}else{
					params.success(res)
				}

			},
			fail: (e) => {
				params.fail(e)
			},
			error: () => {
				console.log("request error")
			},
			complete: (e) => {
				if (e.data.code == 500) {
					params.success(e)
				}
			}
		});
	}

}

Vue.prototype.total_request = function(params) {

	params.method = params.method == undefined ? "GET" : params.method;

	let oriHeader = {}
	if (params.header) {
		oriHeader = params.header
	}
	params.header = Object.assign({
		// 'id_type':'user',
		'Content-Type': 'application/json;charset=utf-8',
		'token': getAccToken(),
	}, oriHeader)
	//在请求头中携带用户token信息
	if (!getAccToken()) {

	} else {
		uni.request({
			url: this.totalUrl + params.url,
			data: params.data,
			header: params.header,
			method: params.method,
			success: (res) => {
				//code === 200
				if (res.data.code == 200 || res.data.code == 0) {
					params.success(res)
				}

				//刷新用户token
				if (res.data.code == 403) {

				}

			},
			fail: (e) => {
				console.log("1111111")
				params.success(e)
			},
			error: () => {
				console.log("request error")
			},
			complete: (e) => {

				params.success(e)
			}
		});
	}

}


Vue.use(uView);

const app = new Vue({
	...App
})
app.$mount()